package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    OrderService orderService;
    @Reference
    SetMealService setMealService;
    @Reference
    MemberService memberService;
    @Autowired
    JedisPool jedisPool;

    /**
     * 通过预约id查询对应的会员,套餐
     *
     * @param id 预约id
     * @return 返回会员对象, 套餐对象, 预约时间, 预约类型
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Order order = orderService.findByid(id);
            Integer memberId = order.getMemberId();
            Member member = memberService.findByid(memberId);
            Integer setmealId = order.getSetmealId();
            Setmeal setmeal = setMealService.findById(setmealId);
            Date orderDate = order.getOrderDate();
            String orderType = order.getOrderType();
            Map<String, Object> map = new HashMap<>();
            map.put("member", member);
            map.put("setmeal", setmeal);
            map.put("orderDate", orderDate);
            map.put("orderType", orderType);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 发送登陆短信验证码
     * @param tel   手机号码
     * @return      发送结果flag
     */
    @RequestMapping("/send4Login")
    public Result send4Login(@RequestParam("telephone") String tel) {
        try {
            jedisPool.getResource().setex(tel+ RedisMessageConstant.SENDTYPE_LOGIN, 30, "123456");
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

}
